#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <cstdlib>
#include <cstdint>

using namespace std;

class Solution {
public:
    int reverse(int x) {
        int64_t y = x;
        if (!(y <= INT32_MAX && y >= INT32_MIN)){
            return 0;
        }
        if (x < 0){
            y = -(int64_t)x;
        }
        vector<int>num;
        while(y / 10){
            num.push_back(y % 10);
            y = y / 10;
        }
        num.push_back(y);
        y = 0;
        for (int i = 0; i < num.size(); i++){
            if (i){
                y *= 10;
            }
            y += num[i];
        }
        if (x < 0){
            y = -y;
        }
        if (y <= INT32_MAX && y >= INT32_MIN){
            return y;
        }else{
            return 0;
        }
    }
};

int main(void)
{
    Solution su;
    int x = -2147483648;
    int y = su.reverse(x);
    printf("reversed x is %d\r\n", y);
    return 0;
}
